package com.woniu.adapter;


import com.alibaba.fastjson.JSON;
import com.woniu.client.WorkClient;
import com.woniu.inlet.vo.WorkingProcedureVo;
import com.woniu.intlet.vo.WorkerVo;
import com.woniu.outlet.dao.*;
import com.woniu.outlet.po.*;
import com.woniu.uitl.MessageStatus;
import com.woniu.uitl.ProScheduingStatus;
import com.woniu.uitl.ResponseResult;
import com.woniu.uitl.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 生产排程适配层
 */
@Component
@Transactional
public class ProSchedulingAdapter {

    @Autowired
    private ProSchedulingPoMapper proSchedulingPoMapper;
    @Autowired
    private ScheWorkMapper scheWorkMapper;
    @Autowired
    private MessagePoMapper messagePoMapper;
    @Autowired
    private WorkClient systemClient;


    //添加生产排程
    public void addProSche(ProSchedulingPo po) throws Exception{
        SnowflakeIdGenerator idWorkes = new SnowflakeIdGenerator(1, 1);
        long id = idWorkes.nextId();
        po.setId(id);
        po.setStatus(ProScheduingStatus.UNSUBMITTED.getStatus());
        proSchedulingPoMapper.insertSelective(po);

        //添加中间表信息
        List<String> workCodes = po.getWorkCodes();
        if (workCodes.size() != 0){
            for (String workCode : workCodes) {
                //查询工序信息
                ResponseResult<WorkingProcedureVo> woPr = systemClient.getByCode(workCode);
                WorkingProcedureVo woPro = woPr.getData();
                if (!StringUtils.isEmpty(woPro)){
                    ScheWork scheWork = new ScheWork();
                    scheWork.setProScheId(po.getId());
                    scheWork.setWorkProId(woPro.getId());
                    scheWork.setProNum(woPro.getProSpecifications() * po.getOrderNum());
                    scheWorkMapper.insertSelective(scheWork);
                }
            }
        }
    }

    //删除生产排程
    public void deleteProSche(Long id) throws Exception{
        ProSchedulingPo proSchedulingPo = proSchedulingPoMapper.selectByPrimaryKey(id);
        if (!proSchedulingPo.getStatus().equals(ProScheduingStatus.UNSUBMITTED.getStatus())){
            throw new Exception("该生产排程状态不支持此次操作");
        }
        //删除生产排程
        proSchedulingPoMapper.deleteByPrimaryKey(id);
        //删除生产排程下的工艺
        ScheWorkExample workExample = new ScheWorkExample();
        ScheWorkExample.Criteria criteria = workExample.createCriteria();
        criteria.andProScheIdEqualTo(id);
        scheWorkMapper.deleteByExample(workExample);
    }

    //修改生产排程
    public void updateProSche(ProSchedulingPo po) throws Exception{
        proSchedulingPoMapper.updateByPrimaryKeySelective(po);
    }

    //提交生产排程.
    public void updateStatus(Long proScheId,String status) throws Exception{
        ProSchedulingPo proSchedulingPo = new ProSchedulingPo();
        proSchedulingPo.setId(proScheId);
        proSchedulingPo.setStatus(status);
        proSchedulingPoMapper.updateByPrimaryKeySelective(proSchedulingPo);

        //发送消息队列给生产人员
        if (status.equals(ProScheduingStatus.UNCOMPLETE.getStatus())){
            ProSchedulingPo proSchedulingPo1 = proSchedulingPoMapper.selectByPrimaryKey(proScheId);
            String proJson = JSON.toJSONString(proSchedulingPo1);
            ResponseResult<WorkerVo> workerById = systemClient.getWorkerById(Long.parseLong(proSchedulingPo1.getWorkerId().toString()));

            WorkerVo worker = workerById.getData();
            String workerJson = JSON.toJSONString(worker);
            MessagePo messagePo = new MessagePo();
            messagePo.setVersion(1L);
            messagePo.setStatus(MessageStatus.UNSUCESS.getStatus());
            messagePo.setExchange("pro_exchange");
            messagePo.setRetryCount(10);
            messagePo.setRoutingKey("pro.plan");
            messagePo.setMessageContent(proJson+"-"+workerJson);
            messagePoMapper.insert(messagePo);
        }
    }

}
